package grpcapi

import (
	"fmt"
	"gitee.com/banyanhouse/distill-infra/apigrpc"
	"gitee.com/banyanhouse/distill-infra/register"
	"gitee.com/banyanhouse/distill-micro-grpc-hystrix/micro_prod_server/ServiceImpl"
	"gitee.com/banyanhouse/distill-micro-grpc-hystrix/micro_prod_server/Services"
	log "github.com/sirupsen/logrus"
)

func init() {
	apigrpc.RegisterGRpcApi(new(GRpcApi))
}

type GRpcApi struct {
}

func (g *GRpcApi) Init(grpcName, grpcPort string) {
	register.GRpcRegister(grpcName, grpcPort)
	// 获取框架go-micro的micro.Service，其内包含注册中心信息
	grpcService := register.GrpcService()
	// 通过RegisterProdServiceHandler将服务实现方法注册到注册中心中
	err := Services.RegisterProdServiceHandler(grpcService.Server(), new(ServiceImpl.ProdService))
	if err != nil {
		log.Errorf("GRpc Service impl failed:%s\n", err.Error())
	}
	// 打印查看中策中心的服务列表清单
	services, err := register.RegistryCenter().ListServices()
	if err != nil {
		log.Errorf("get registry service list failed. %s", err.Error())
	}
	for i, service := range services {
		fmt.Printf("%d -----> %s\n", i, service.Name)
	}
}

func (g *GRpcApi) Setup(grpcName, grpcPort string) {
	//cmd.Init()
	//md := server.DefaultOptions().Metadata
	//md["datacenter"] = "local"
	//
	//server.DefaultServer = server.NewServer(
	//	server.Metadata(md),
	//)
}
